package com.audaque.springboot.foshanupload.web.webdemo.controller;

import cn.hutool.json.JSONUtil;
import com.audaque.springboot.foshanupload.log.anno.WebLogAnno;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static cn.hutool.http.ContentType.JSON;

@Slf4j
@RestController
@RequestMapping("objectMapper")
public class ObjectMapperController {

    @Autowired
    private ObjectMapper objectMapper;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @ToString
    private static class Coordinate1 {
        //@JsonProperty是可以在序列化和反序列化中使用
        @JsonProperty(value = "x_axis", index = 1, access = JsonProperty.Access.WRITE_ONLY)
        private String xAxis;
        @JsonProperty(value = "y_axis", index = 2, access = JsonProperty.Access.READ_WRITE)
        private String yAxis;
        @JsonProperty(value = "z_axis", index = 3, access = JsonProperty.Access.READ_WRITE)
        private String zAxis;
    }

    @WebLogAnno
    @GetMapping("test1")
    public void test1() throws Exception {
        Coordinate1 coordinate = Coordinate1.builder()
                .xAxis("113.58")
                .yAxis("37.86")
                .zAxis("40.05")
                .build();
        String jsonStr = JSONUtil.toJsonStr(coordinate);
        log.debug("serializes the specified object into its equivalent Json representation :" + jsonStr);
        try {
            String str = objectMapper.writeValueAsString(coordinate);
            log.debug("serialize any Java value as a String : " + str);
            Object bean = objectMapper.readerFor(Coordinate1.class).readValue(str);
            log.debug("read or update instances of specified type : " + bean);
        } catch (JsonProcessingException e) {
            log.error("error message : " + e);
        }

    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @ToString
    private static class Coordinate2 {
        //@JsonAlias只在反序列化中起作用，指定Java属性可以接受的更多名称
        @JsonAlias(value = "x_location")
        //@JsonProperty是可以在序列化和反序列化中使用
        @JsonProperty(value = "x_axis")
        private String xAxis;
        @JsonProperty(value = "y_axis")
        @JsonAlias(value = "y_location")
        private String yAxis;
        @JsonProperty(value = "z_axis")
        @JsonAlias(value = "z_location")
        private String zAxis;
    }

    @WebLogAnno
    @GetMapping("test2")
    public void test2() throws Exception {
        String location = "{\"x_location\":\"113.58\",\"y_location\":\"37.86\",\"z_location\":\"40.05\"}";
        try {
            Object bean = objectMapper.readValue(location, Coordinate2.class);
            log.debug("read or update instances of specified type : " + bean);
        } catch (JsonProcessingException e) {
            log.error("error message : " + e);
        }

    }
}
